package com.lbj.core.ext

import androidx.appcompat.app.AppCompatActivity
import com.lbj.core.app.CoreActivity
import com.lbj.core.app.CoreFragment
import com.lbj.mvvm.network.AppException
import com.lbj.mvvm.work.Work

/**
 * 作者：李炳军
 * 创建日期：2021-03-29  15:39
 * 释义：
 */

/**
 * 显示页面状态，这里有个技巧，成功回调在第一个，其后两个带默认值的回调可省
 * [work] 接口返回值
 * [onLoading] 加载中
 * [onSuccess] 成功回调
 * [onError] 失败回调
 */
fun <T> AppCompatActivity.runTest(
    work: Work<T>,
    onSuccess: (T) -> Unit,
    onError: ((AppException) -> Unit),
    onLoading: (() -> Unit)? = null
) {
    when (work) {
        is Work.Loading -> {
            onLoading?.run { this }
        }
        is Work.Success -> {

            onSuccess(work.data)
        }
        is Work.Error -> {
            onError.run {
                this(work.error)
            }
        }
    }
}

/**
 * 显示页面状态，这里有个技巧，成功回调在第一个，其后两个带默认值的回调可省
 * [work] 接口返回值
 * [onLoading] 加载中
 * [onSuccess] 成功回调
 * [onError] 失败回调
 */
fun <T> CoreActivity<*, *>.runWork(
    work: Work<T>,
    onSuccess: (T) -> Unit,
    onError: ((AppException) -> Unit)? = null,
    onLoading: (() -> Unit)? = null
) {
    when (work) {
        is Work.Loading -> {
            showLoading(work.loadingMessage)
            onLoading?.run { this }
        }
        is Work.Success -> {
            dismissLoading()
            onSuccess(work.data)
        }
        is Work.Error -> {
            dismissLoading()
            onError?.run {
                this(work.error)
            }
        }
    }
}

fun <T> CoreActivity<*, *>.runWorkMsg(
    work: Work<T>,
    onSuccess: (T, Int, String) -> Unit,
    onError: ((AppException) -> Unit),
    onLoading: (() -> Unit)? = null
) {
    when (work) {
        is Work.Loading -> {
            showLoading(work.loadingMessage)
            onLoading?.run { this }
        }
        is Work.Success -> {
            dismissLoading()

            onSuccess(work.data, work.code, work.msg)
        }
        is Work.Error -> {
            dismissLoading()
            onError.run {
                this(work.error)
            }
        }
    }
}


fun <T> CoreFragment<*,*>.runWork(
    work: Work<T>,
    onSuccess: (T) -> Unit,
    onError: ((AppException) -> Unit),
    onLoading: (() -> Unit)? = null
) {
    when (work) {
        is Work.Loading -> {
            showLoading(work.loadingMessage)
            onLoading?.run { this }
        }
        is Work.Success -> {
            dismissLoading()
            onSuccess(work.data)
        }
        is Work.Error -> {
            dismissLoading()
            onError.run { this(work.error) }
        }
    }
}

fun <T> CoreFragment<*, *>.runWorkMsg(
    work: Work<T>,
    onSuccess: (T, Int, String) -> Unit,
    onError: ((AppException) -> Unit),
    onLoading: (() -> Unit)? = null
) {
    when (work) {
        is Work.Loading -> {
            showLoading(work.loadingMessage)
            onLoading?.run { this }
        }
        is Work.Success -> {
            dismissLoading()

            onSuccess(work.data, work.code, work.msg)
        }
        is Work.Error -> {
            dismissLoading()
            onError.run {
                this(work.error)
            }
        }
    }
}